using System.Collections;
using System.Collections.Generic;
using ProjectSurviver;
using QFramework;
using UnityEngine;

public class CoinUpgradeSystem : AbstractSystem, ICanSave
{
    public static EasyEvent OnCoinUpgradeSystemChange = new EasyEvent();

    public List<CoinUpgradeItem> Items { get; } = new List<CoinUpgradeItem>();

    public CoinUpgradeItem Add(CoinUpgradeItem item)
    {
        Items.Add(item);
        return item;
    }

    protected override void OnInit()
    {
        var coinPercentLv1 = Add(new CoinUpgradeItem()
        .WithKey("coin_percent_lv1")
        .WithDescription("金币掉落概率提升 Lv1")
        .WithPrice(5)
        .OnUpgrade((item) =>
        {
            Global.CoinPercent.Value += 0.1f;
            Global.Coin.Value -= item.Price;
        }));

        var coinPercentLv2 = Add(new CoinUpgradeItem()
        .WithKey("coin_percent_lv2")
        .WithDescription("金币掉落概率提升 Lv2")
        .WithPrice(7)
        .Condition((_) => coinPercentLv1.UpgradeFinish)
        .OnUpgrade((item) =>
        {
            Global.CoinPercent.Value += 0.1f;
            Global.Coin.Value -= item.Price;
        }));

        coinPercentLv1.OnChange.Register(() =>
        {
            coinPercentLv2.OnChange.Trigger();
        });

        var coinPercentLv3 = Add(new CoinUpgradeItem()
        .WithKey("coin_percent_lv3")
        .WithDescription("金币掉落概率提升 Lv3")
        .WithPrice(10)
        .Condition((_) => coinPercentLv2.UpgradeFinish)
        .OnUpgrade((item) =>
        {
            Global.CoinPercent.Value += 0.1f;
            Global.Coin.Value -= item.Price;
        }));

        coinPercentLv2.OnChange.Register(() =>
        {
            coinPercentLv3.OnChange.Trigger();
        });

         Items.Add(new CoinUpgradeItem()
        .WithKey("exp_percent")
        .WithDescription("经验值掉落概率提升")
        .WithPrice(5)
        .OnUpgrade((item) =>
        {
            Global.ExpPercent.Value += 0.1f;
            Global.Coin.Value -= item.Price;
        }));

         Items.Add(new CoinUpgradeItem()
        .WithKey("max_hp")
        .WithDescription("主角的最大血量+1")
        .WithPrice(30)
        .OnUpgrade((item) =>
        {
            Global.MaxHP.Value ++;
            Global.Coin.Value -= item.Price;
        }));

        Load();

        OnCoinUpgradeSystemChange.Register(() =>
        {
            Save();
        });
    }

    public void Say()
    {
        Debug.Log("Hello CoinUpgradeSystem");
    }

    public void Save()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            saveSystem.SaveBool(coinUpgradeItem.Key, coinUpgradeItem.UpgradeFinish);
        }
    }

    public void Load()
    {
        var saveSystem = this.GetSystem<SaveSystem>();
        foreach (var coinUpgradeItem in Items)
        {
            coinUpgradeItem.UpgradeFinish = saveSystem.LoadBool(coinUpgradeItem.Key, false);
        }
    }
}
